package com.sky.controller.user;


import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("购物车相关接口：")
@Slf4j
public class ShopCartController {

    @Autowired
    private ShopCartService shopCartService;


    /**
     * 加入购物车
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("加入购物车")
    @PostMapping("/user/shoppingCart/add")
    public Result addShopCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("插入购物车");

        shopCartService.addShopCart(shoppingCartDTO);

        return Result.success();
    }

    /**
     * 查询购物车所有数据
     * @return
     */
    @ApiOperation("查看购物车")
    @GetMapping("/user/shoppingCart/list")
    public Result<List<ShoppingCart>> List(){
        log.info("查看购物车");

        List<ShoppingCart> list= shopCartService.getList();
        return Result.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/user/shoppingCart/clean")
    @ApiOperation("删除购物车所有数据")
    public Result cleanShopCart() {
        log.info("删除购物车所有数据");
        shopCartService.deleteAll();

        return Result.success("删除成功！");
    }


    /**
     * 删除购物车商品数量
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/user/shoppingCart/sub")
    @ApiOperation("删除购物车商品数量")
    public Result deleteShopCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车商品");
        shopCartService.delete(shoppingCartDTO);
        return Result.success("删除成功！");
    }
}
